module.exports = {
    configureWebpack: {
        devServer: {
            // mockjs拦截数据返回的设置 
            before(app) {

                //模拟用户列表接口信息+分页
                var userlist = [{
                    "id": 10,
                    "UserName": "zs1",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "18888888888",
                    "Email": "405328555@qq.com",
                    "MgState": true
                }, {
                    "id": 9,
                    "UserName": "zs2",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "18888888888",
                    "Email": "405328555@qq.com",
                    "MgState": true
                }, {
                    "id": 8,
                    "UserName": "zs3",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "18888888888",
                    "Email": "405328555@qq.com",
                    "MgState": true
                }, {
                    "id": 7,
                    "UserName": "zs4",
                    "Password": "555555",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13312311231",
                    "Email": "55@qq.com",
                    "MgState": true
                }, {
                    "id": 6,
                    "UserName": "zs5",
                    "Password": "333333",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13388888888",
                    "Email": "33@qq.com",
                    "MgState": true
                }, {
                    "id": 5,
                    "UserName": "zs6",
                    "Password": "222222",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13845644564",
                    "Email": "22@qq.com",
                    "MgState": true
                }, {
                    "id": 4,
                    "UserName": "zs7",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "",
                    "Email": "",
                    "MgState": true
                }, {
                    "id": 3,
                    "UserName": "zs8",
                    "Password": "",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13812341235",
                    "Email": "888888@qq.com",
                    "MgState": true
                }, {
                    "id": 2,
                    "UserName": "zs9",
                    "Password": "",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13812341888",
                    "Email": "288888@qq.com",
                    "MgState": true
                }, {
                    "id": 1,
                    "UserName": "zs10",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "超级管理员",
                    "Mobile": "13602221234",
                    "Email": "223456@qq.com",
                    "MgState": true
                }]

                // 修改用户接口 
                //修改用户信息的接口
                app.get('/api/updateuser', (req, res) => {
                    const { id, username, password, email, mobile, pagenum, pagesize } = req.query
                    if (userlist) {
                        userlist.filter(u => u.id == id).forEach(element => {
                            element.UserName = username
                            element.Email = email
                            element.Mobile = mobile
                        })
                    }
                    var total = userlist.length
                    if (pagenum == 0) {
                        newDataList = userlist.slice(pagenum * pagesize, (parseInt(pagenum) + 1) * pagesize)
                    } else
                        newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)

                    res.json({
                        'status': 'ok',
                        'total': total,
                        'data': newDataList
                    })
                })




                // 删除用户接口实现 
                app.get('/api/deleteuser', (req, res) => {
                    const { id, query, pagenum, pagesize } = req.query
                    for (var i = 0; i < userlist.length; i++) {
                        if (userlist[i].id == id) {
                            userlist.splice(i, 1)
                        }
                    }

                    var total = userlist.length

                    var newdatalist = userlist.slice(
                        (parseInt(pagenum) - 1) * pagesize,
                        (parseInt(pagenum)) * pagesize
                    )
                    res.json({
                        status: 'ok',
                        total: total,
                        data: newdatalist
                    })
                })



                app.get('/api/menu', (req, res) => {
                    res.json({

                        "status": "ok",
                        "data": [{
                            "id": 1,
                            "name": "用户管理",
                            "child": [{
                                "id": 1,
                                "name": "用户列表",
                                "path": "/home/users"
                            }]
                        }, {
                            "id": 2,
                            "name": "员工管理",
                            "child": [{
                                "id": 2,
                                "name": "员工列表",
                                "path": "/home/rote"
                            },]
                        }, {
                            "id": 3,
                            "name": "商品管理",
                            "child": [{
                                "id": 4,
                                "name": "商品列表",
                                "path": "/home/product"
                            }, {
                                "id": 5,
                                "name": "分类参数",
                                "path": "/home/userss"
                            }, {
                                "id": 6,
                                "name": "商品分类",
                                "path": "/home/producttype"
                            }]
                        }, {
                            "id": 4,
                            "name": "订单管理",
                            "child": []
                        }, {
                            "id": 5,
                            "name": "数据统计",
                            "child": []
                        }]
                    })
                })

                // 添加用户数据的接口  
                // mock.js添加用户的接口

                app.get('/api/adduser', (req, res) => {
                    const { username, password, email, mobile, pagenum, pagesize } = req.query
                    var id = userlist[0].id
                    var adduser = {
                        "id": id + 1,
                        "UserName": username,
                        "Password": password,
                        "CreateTime": "0001/1/1 星期一 0:00:00",
                        "RoleName": "",
                        "Mobile": mobile,
                        "Email": email,
                        "MgState": true
                    }
                    userlist.unshift(adduser)

                    total = userlist.length
                    var newDataList = []

                    newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
                    //向前端反馈的数据
                    res.json({
                        'total': total,
                        'data': newDataList
                    })
                })


                // 用户请求的列表显示 并且添加分页信息 
                app.get('/api/users', (req, res) => {
                    const { query, pagenum, pagesize } = req.query
                    var total = userlist.length
                    var newdatalist = userlist.slice(
                        (parseInt(pagenum) - 1) * pagesize,
                        (parseInt(pagenum)) * pagesize
                    )
                    res.json({
                        status: 'ok',
                        total: total,
                        data: newdatalist
                    })
                })




                // 模拟一个登录接口 
                const tokenstr = 'tokenfwefefefwweffewffw'
                app.get('/api/login', (req, res) => {
                    const { username, password } = req.query
                    if (username == 'admin' && password == '123456') {
                        res.json({
                            code: 1,
                            message: '登录成功',
                            token: tokenstr
                        })
                    } else {
                        res.json({
                            code: 0,
                            message: '登录失败'
                        })
                    }
                })


                // 修改用户接口  
                app.get('/api/userupdate', (req, res) => {
                    const { id, MgState } = req.query
                    var newState = ''
                    var user = userlist.filter((u) => u.id == id)

                    user.MgState = MgState
                    newState = user.MgState
                    res.json({
                        code: '200',
                        newState: newState
                    })
                })


                // 搜索用户的接口 
                app.get('/api/usersearch', (req, res) => {
                    const { query } = req.query
                    var newDataList = []
                    if (query != '') {
                        newDataList = userlist.filter((u) => u.UserName == query)
                    }
                    res.json({
                        status: 'ok',
                        data: newDataList
                    })
                })



            }
        }
    }
}